package com.sys.server.modules.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.server.common.config.CommonConfig;
import com.sys.server.common.constants.enums.ResultSetEnum;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.CaseDto;
import com.sys.server.modules.controller.vo.PageVo;
import com.sys.server.modules.entity.CaseEntity;
import com.sys.server.modules.mapper.CaseMapper;
import com.sys.server.modules.service.CaseService;
import com.sys.server.modules.service.FileService;
import com.sys.server.modules.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Service
public class CaseServiceImpl implements CaseService {

    private static final Logger LOG = LoggerFactory.getLogger(CaseServiceImpl.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private TokenService tokenService;

    @Resource
    private CaseMapper caseMapper;

    @Override
    public R addCase(CaseDto caseDto) {
        CaseEntity entity = null;
        if (caseDto.getCid() == 0) {
            entity = new CaseEntity();
        } else {
            entity = caseMapper.selectById(caseDto.getCid());
        }
        entity.setName(caseDto.getName());
        entity.setType(caseDto.getType());
        Date date = new Date();
        LOG.info("时间===" + date);
        entity.setReltime(date);
        entity.setMaintext(caseDto.getMaintext());
        entity.setReluser(tokenService.getCurrentUser());
        /** 文件上传 */
        String paths = CommonConfig.getAppendix_path() + "/case/" + caseDto.getName();
        String path = fileService.fileUpload(caseDto.getFile(), paths);
        if (StringUtils.isNotBlank(path)) {
            entity.setAttachment(path);
        } else {
            LOG.error("获取附件路径失败");
        }
        try {
            caseMapper.insert(entity);
        } catch (Exception e) {
            LOG.error("新增案例错误{}" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    @Override
    public R queryCase(Long pagenum, Long pagesize) {
        IPage<CaseEntity> page = new Page<>(pagenum, pagesize);
        try {
            caseMapper.selectPage(page, null);
        } catch (Exception e) {
            LOG.error("分页查询案例列表错误" + e.getMessage());
        }
        PageVo result = new PageVo();
        result.setCurrent(pagenum);
        result.setTotal(page.getTotal());
        result.setSize(pagesize);
        if (page.getRecords() != null) {
            result.setList(page.getRecords());
        }
        return R.ok(ResultSetEnum.SUCCESS, result);
    }

    @Override
    public String downloadCaseFile(String cid) throws IOException {
        CaseEntity entity = caseMapper.selectById(cid);
        String path = entity.getAttachment();
        return path;
    }

    @Override
    public R getCaseInfoById(Long cid) {
        try {
            CaseEntity entity = caseMapper.selectById(cid);
            if (entity != null) {
                return R.ok(ResultSetEnum.SUCCESS, entity);
            }
        } catch (Exception e) {
            LOG.error("根据id查询案例信息错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    @Override
    public R updateCaseInfo(CaseDto caseDto) {
        long cid = caseDto.getCid();
        try {
            CaseEntity entity = caseMapper.selectById(cid);
            entity.setType(caseDto.getType());
            entity.setName(caseDto.getName());
            entity.setMaintext(caseDto.getMaintext());
            try {
                caseMapper.update(entity, Wrappers.<CaseEntity>query().eq("cid", cid));
                return R.ok(ResultSetEnum.SUCCESS);
            } catch (Exception e) {
                LOG.error("更新案例信息（未上传附件）更新时错误" + e.getMessage());
                return R.ok(ResultSetEnum.FAIL);
            }
        } catch (Exception e) {
            LOG.error("更新案例信息（未上传附件）查询时错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
    }

    @Override
    public R deleteCase(Long cid) {
        try {
            caseMapper.deleteById(cid);
        } catch (Exception e) {
            LOG.error("删除案例错误" + e.getMessage());
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

}
